package com.cloudm.technician.ui.modules.scan_qr_code

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Vibrator
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import cn.bingoogolapple.qrcode.core.QRCodeView
import cn.bingoogolapple.qrcode.zbar.ZBarView
import com.cloudm.technician.R
import com.cloudm.technician.extensions.toast
import com.cloudm.technician.ui.base.BaseFragment
import kotlinx.android.synthetic.main.fragment_scan_qr_code.*

/**
 * 作者: wesley
 * 时间: 2017/12/21
 * 描述: ui
 */
class ScanQrCodeFragment : BaseFragment<ScanQrCodePresenter>(), ScanQrCodeContract.View,
        QRCodeView.Delegate, TextWatcher {

    private var zxingView: ZBarView? = null
    private var flag: Int = -1

    override val resource: Int
        get() = R.layout.fragment_scan_qr_code

    companion object {
        fun newInstance(flag: Int) = ScanQrCodeFragment().apply {
            arguments = Bundle().apply {
                putInt("flag", flag)
            }
        }
    }

    override fun initInject() {
        DaggerScanQrCodeComponent.builder().appComponent(appComponent).scanQrCodePresenterModule(
                ScanQrCodePresenterModule(this)).build().inject(this)
    }

    override fun handleBundle(bundle: Bundle) {
        flag = bundle.getInt("flag")
    }

    override fun initVariables() {

    }

    override fun initViews(view: View) {
        zxingView = view.findViewById(R.id.zxingView)
        zxingView?.setDelegate(this)
        zxingView?.startSpotAndShowRect()
    }

    override fun initListener() {
        checkbox.setOnCheckedChangeListener { _, isChecked ->
            when (isChecked) {
                true -> zxingView?.openFlashlight()
                false -> zxingView?.closeFlashlight()
            }
        }

        etScanResult.addTextChangedListener(this)
    }

    override fun onPause() {
        checkbox.isChecked = false
        super.onPause()
    }

    override fun onStop() {
        zxingView?.stopCamera()
        super.onStop()
    }

    override fun onDestroy() {
        zxingView?.onDestroy()
        super.onDestroy()
    }

    override fun onScanQRCodeSuccess(result: String?) {
        onScanSuccess(result)
    }

    override fun onScanQRCodeOpenCameraError() {
        mContext.toast(mContext.resources.getString(R.string.open_camear_error))
    }

    @Suppress("DEPRECATION") private fun vibrate() {
        val vibrator = mContext.getSystemService(Context.VIBRATOR_SERVICE) as? Vibrator ?: return
        if (vibrator.hasVibrator()) {
            vibrator.vibrate(200)
        }
    }

    override fun afterTextChanged(s: Editable?) {

    }

    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

    }

    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
        presenter?.onTextChanged(s?.toString(), flag)
    }

    override fun scanSuccess(value: String) {
        onScanSuccess(value)
    }

    private fun onScanSuccess(result: String?) {
        vibrate()
        zxingView?.stopSpot()
        val activity = mContext as? ScanQrCodeActivity ?: return
        val intent = Intent().apply {
            putExtras(Bundle().apply {
                putString("result", result)
            })
        }
        activity.setResult(Activity.RESULT_OK, intent)
        activity.finish()
    }

}